package com.vdong.trade.trading.service.impl;

import com.vdong.trade.trading.common.enums.SysTypeEnum;
import com.vdong.trade.trading.entity.dto.param.AmountTradingParamDTO;
import com.vdong.trade.trading.entity.mo.TradingMO;
import com.vdong.trade.trading.respository.TradingMOMapper;
import com.vdong.trade.trading.service.TradingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
@Slf4j
public class TradingServiceImpl implements TradingService {

    @Autowired
    private TradingMOMapper tradingMOMapper;

    @Override
    public Optional<String> insertTrading(AmountTradingParamDTO paramDTO) {
        TradingMO tradingMO = new TradingMO();
        CopyUtil.copyObject(paramDTO, tradingMO);

        if (StringUtil.hasBlank(paramDTO.getAccountEnter())) {
            tradingMO.setAccountEnter(0L);
        }
        if (StringUtil.hasBlank(paramDTO.getAccountOut())) {
            tradingMO.setAccountOut(0L);
        }
        if (StringUtil.hasBlank(paramDTO.getOrderNo())) {
            tradingMO.setOrderNo(StringUtils.EMPTY);
        }
        if (StringUtil.hasBlank(paramDTO.getSysType())) {
            tradingMO.setSysType(SysTypeEnum.DISB.getValue().toString());
        }
        if (StringUtil.hasBlank(paramDTO.getWechatDataId())) {
            tradingMO.setWechatDataId(0L);
        }

        tradingMO.setCreateTime(new Date());
        tradingMOMapper.insertTrading(tradingMO);
        return Optional.empty();
    }
}
